package com.thinking.project.monitorserver.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.thinking.project.monitorserver.handler.TokenHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * @Description 功能描述：  设置接口访问权限 设置需要拦截的请求前缀
 * @Author tangsixiang@163.com
 * @DATE 2019/6/24 11:17
 * @Version 1.0
 **/
@Configuration
@EnableWebMvc
public class WebAppConfigurer implements WebMvcConfigurer {

     @Autowired
    TokenHandler tokenHandler;
    /**
     * @return void
     * @Author tangsixiang
     * @Description 修改默认 返回前端 json格式  可以默认去掉为空的属性
     * @Date 10:06 2019/6/26
     * @Param [converters]
     ****/
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        //创建FastJson的消息转换器
        FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
        //创建FastJson的配置对象
        FastJsonConfig config = new FastJsonConfig();
        //对Json数据进行格式化  将为null的字段值显示为""  数值字段如果为null,输出为0,而非null
        config.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullNumberAsZero);
        convert.setFastJsonConfig(config);

        converters.add(convert);
}

     @Override
     public void addCorsMappings(CorsRegistry registry) {
         registry.addMapping("/**")
                 .allowedOrigins("*")
                 .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
                 .allowCredentials(true).maxAge(3600);
     }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    /***
     *   采用 spring mvc 拦截器  过滤需要鉴权资源   建议默认拦截全部请求   在  tokenHandler中判断是否需要连接
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截的管理器
        // 拦截的对象会进入这个类中进行判断
        InterceptorRegistration registration = registry.addInterceptor(tokenHandler);
        //所有路径都被拦截
        registration.addPathPatterns("/**");

        //除去不连接地址为
        registration.excludePathPatterns("/login/**");
    }
}
